package com.jzo2o.health.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.trade.TradingApi;
import com.jzo2o.api.trade.dto.response.TradingResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @author xd
 */
@Component
@Slf4j
public class TradingClient {
    @Resource
    private TradingApi tradingApi;

    /**
     * 远程调用支付服务查询支付结果
     * @param tradingOrderNo {@code Long}交易订单号
     * @return {@code TradingResDTO}交易数据对象
     */
    @SentinelResource(value ="getTradResult",fallback = "getTradResultFallback",blockHandler = "getTradResultBlockHandler")
    public TradingResDTO getTradResult(@RequestParam("tradingOrderNo") Long tradingOrderNo){
       return tradingApi.findTradResultByTradingOrderNo(tradingOrderNo);
    }

    /**
     * 异常降级处理
     * @param tradingOrderNo {@code Long}交易订单号
     * @param t {@code Throwable}普通异常
     * @return null
     */
    public TradingResDTO getTradResultFallback(@RequestParam("tradingOrderNo") Long tradingOrderNo,Throwable t){
        log.error("远程调用查询支付结果出现异常,交易订单号为{}",tradingOrderNo,t);
        return null;
    }

    /**
     * 熔断降级处理
     * @param tradingOrderNo {@code Long}交易订单号
     * @param e {@code BlockException}熔断异常
     * @return null
     */
    public TradingResDTO getTradResultBlockHandler(@RequestParam("tradingOrderNo") Long tradingOrderNo, BlockException e){
        log.error("远程调用查询支付结果被熔断,交易订单号为{}",tradingOrderNo,e);
        return null;
    }
}
